/**
 * DrawerEdgeMouseListener.java
 *
 * Created on Feb 8, 2007 at 1:07:11 PM
 *
 * Copyright 2008 the Desert Labs Project (http://desertlabs.us)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package us.desertlabs.ui.drawer;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


/**
 * This listener opens and closes the <CODE>Drawer</CODE> when the user clicks on
 * the edge.  Because of quirks in Java's idea of a "click", it does some slightly
 * involved state management.
 *
 * @author Brandon Franklin
 */
public class DrawerEdgeMouseListener extends MouseAdapter {

    /** The <CODE>Drawer</CODE> that this listener controls */
    final private Drawer drawer;

    /** Tracks when the user has pressed the mouse without leaving the edge's bounds */
    private boolean pressed = false;


    /**
     * Creates a new <CODE>DrawerEdgeMouseListener</CODE> and prepares it for use.
     *
     * @param drawer the <CODE>Drawer</CODE> that this listener controls
     */
    public DrawerEdgeMouseListener(final Drawer drawer) {
        this.drawer = drawer;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void mouseExited(final MouseEvent e) {
        super.mouseExited(e);
        pressed = false;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void mousePressed(final MouseEvent e) {
        super.mousePressed(e);
        pressed = true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void mouseReleased(final MouseEvent e) {
        super.mouseReleased(e);
        if( pressed ) {
            drawer.setOpen( !drawer.isOpen() );
            drawer.revalidate();
        }
    }

}
